import { Controller, Get, Post, Body, Request, Param, Delete, UseGuards, Req } from "@nestjs/common";
import { AuthService } from './auth.service';
import { RoleService } from "@/modules/role/role.service";
import { LocalGuard } from "@/common/guards";
import { JwtGuard } from "@/common/guards/jwt.guard";

@Controller('auth')
export class AuthController {
  constructor(
    private readonly authService: AuthService,
    private readonly roleService: RoleService,
  ) {}

  //登录
  @Post('login')
  @UseGuards(LocalGuard)
  async login(@Req() req: any) {
    return this.authService.login(req.user, req);
  }

  // 退出登录
  @Post('logout')
  @UseGuards(JwtGuard)
  logout(@Request() req: any) {
    return this.authService.logout(req.user);
  }

  // 无感token刷新
  @Post('refreshToken')
  refreshToken(@Body() refreshToken: string) {
    return this.authService.refreshToken(refreshToken);
  }

  // 切换用户当前角色-使用redis记录
  @Post('switchRole')
  @UseGuards(JwtGuard) // 需要鉴权
  switchRole(@Req() req: any, @Body('roleCode') roleCode: string) {
    // 从req中获取用户信息，然后调用service层的方法，根据角色code切换用户当前角色
    return this.roleService.switchRole(req, roleCode);
  }

  // 获取用户动态菜单
  @Get('userPermission')
  @UseGuards(JwtGuard)
  getUserPermission(@Req() req: any) {
    return this.roleService.getUserPermission(req);
  }
}
